<?

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

getMyParam('typeselect');
getMyParam('select2');

if ($typeselect == 1) {
    $sql = "SELECT `name` FROM `category` WHERE `id` = '$select2'";
    $tmp_ary = $db->get_one($sql);
    $file_name = $tmp_ary['name'] . ".xls";

    header("Content-type:application/vnd.ms-excel");
    header("Accept-Ranges:bytes");
    header("Content-Disposition:attachment;filename=".$file_name);
    //echo "<pre>";

    $sql = "SELECT `id`, `code`, `fid`, `name`, `pic`, `seq`, `description` FROM `category` WHERE `fid` = '$select2' ORDER BY `code` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $sub_cat_ary[$rs['id']] = $rs;
        $sub_cat_ary1[] = $rs['id'];
    }

    foreach ($excel_sql_ary as $k=>$v) {
        echo $k . "\t";
    }

    $cat_index_ary = array();
    if (!empty($sub_cat_ary)) {
        foreach ($sub_cat_ary as $k=>$v) {
            echo $v['code'] . " " . $v['name'] . "\t";
            $cat_index_ary[] = $k;
        }
    }

    $have_product_id_ary = array();

    $get_cat_ary = array();
    $have_product_id_ary = array();

    if (!empty($sub_cat_ary1)) {
        $sub_cat_str = implode(",", $sub_cat_ary1);
    } else {
        $sub_cat_str = $select2;
    }
    $sql = "SELECT `product_id`, `category_id`, `seq` FROM `product_category` WHERE `category_id` IN (" . $sub_cat_str . ")";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $get_cat_ary[$rs['product_id']][$rs['category_id']] = $rs['seq'];
        $have_product_id_ary[] = $rs['product_id'];
    }


    $have_product_id_str = implode(",", $have_product_id_ary);
    //$have_product_id_str = 4;

    $sql = "SELECT * FROM `product` WHERE `id` IN (" . $have_product_id_str . ") ORDER BY `id` ASC";
    //echo $sql;
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        echo "\n";
        foreach ($excel_sql_ary as $k=>$v) {
            $rs[$v] = htmlentities($rs[$v]);
            if ($v == "hidden_flag") {
                $rs[$v] = $rs[$v] == 1 ? "n" : "y";
            } elseif ($v == "new_flag") {
                $rs[$v] = $rs[$v] == 1 ? "y" : "";
            } elseif ($v == "name") {
                //$name = $rs['name'];
                //$name = html_entity_decode($name);
                //echo "<hr>" . $name;exit;

                //$rs[$v] = html_entity_decode($rs[$v]);
            }

            echo $rs[$v] . "\t";
        }

        foreach ($cat_index_ary as $k=>$v) {
            if (@isset($get_cat_ary[$rs['id']][$v])) {
                echo $get_cat_ary[$rs['id']][$v] . "\t";
            } else {
                echo "\t";
            }
        }

    }

    exit;
} elseif ($typeselect == 2) {
    $sql = "SELECT `name`, `type` FROM `inventory` WHERE `id` = '$select2'";
    $tmp_ary = $db->get_one($sql);
    $file_name = "Inventory_" . $tmp_ary['name'] . ".xls";
    $inventory_type = $tmp_ary['type'];
    $inventory_name = $tmp_ary['name'];

    header("Content-type:application/vnd.ms-excel");
    header("Accept-Ranges:bytes");
    header("Content-Disposition:attachment;filename=".$file_name);

    if ($inventory_type == 1) {
        echo "Item#\tQty\tSpecial Price\tSpecial Price2\tLocation";
    } else {
        echo "Item#\tQty\tSpecial Price\tLocation";
    }

    $sql = "SELECT `product_id`, `inventory_id`, `qty`, `special_price`, `special_price2` FROM `product_inventory` WHERE `inventory_id`='$select2'";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $inventory_info_ary[$rs['product_id']] = $rs;
        $product_id_ary[] = $rs['product_id'];
    }

    $product_id_ary = array_unique($product_id_ary);
    $product_id_str = implode(",", $product_id_ary);
    $sql = "SELECT * FROM `product` WHERE `id` IN (" . $product_id_str . ") ORDER BY `id` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        echo "\n";
        echo $rs['code'] . "\t";
        echo $inventory_info_ary[$rs['id']]['qty'] . "\t";
        echo $inventory_info_ary[$rs['id']]['special_price'] . "\t";
        if ($inventory_type == 1) {
            echo $inventory_info_ary[$rs['id']]['special_price2'] . "\t";
        }
        echo $inventory_name . "\t";
    }

    exit;

}

?> 